<?php

namespace App\Console\Commands;

use App\Models\Bus\BusIsCheck;
use Illuminate\Console\Command;

class buildCarIsCheckData extends Command
{
    /**
     * The name and signature of the console command.
     *
     * @var string
     */
    protected $signature = 'buildCarIsCheckData';

    /**
     * The console command description.
     *
     * @var string
     */
    protected $description = '自动填充车辆是否考核数据1';

    /**
     * Create a new command instance.
     *
     * @return void
     */
    public function __construct()
    {
        parent::__construct();
    }

    /**
     * Execute the console command.
     *
     * @return mixed
     */
    public function handle()
    {
        // 获取最后一个月数据
        $month = BusIsCheck::query()->select('month')->groupBy('month')->orderByDesc('month')->pluck('month')->first();
        $month .= '-01';
        $latestMonth = BusIsCheck::query()->where('month', $month)->get()->toArray();
        foreach ($latestMonth as $k => $v) {
            unset($latestMonth[$k]['id']);
            $latestMonth[$k]['month'] = date('Y-m-d', strtotime('+1 month', strtotime($v['month'].'-01')));
        }
        // 填充新的数据
        $result = BusIsCheck::query()->insert($latestMonth);
        $this->info($result ? 'Success' : 'Fail');
    }
}
